package weifei.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import weifei.mall.entity.Goods;
import weifei.mall.entity.GoodsPicture;
import weifei.web.Result;

@RestController
@RequestMapping("/goods")
public class GoodsController
{

    @Autowired
    private Goods dao_goods;
    @Autowired
    private GoodsPicture dao_goods_picture;


    @GetMapping("/getGoodsByID")
    @PostMapping("/getGoodsByID")
    public Result getGoodsByID(@RequestParam("id") Integer id)
    {
        if (id == null)
            return Result.exception("参数错误");

        QueryWrapper<Goods> qw_goods = new QueryWrapper<>();
        qw_goods.eq(Goods.Column.ID.getName(), id);
        var goods = this.dao_goods.selectOne(qw_goods);
        if (goods == null)
            return Result.failed("商品不存在");
        return Result.success("获取成功", goods);
    }


    @GetMapping(value = "/getGoodsCoverByGoodsID", produces = MediaType.IMAGE_JPEG_VALUE)
    @PostMapping(value = "/getGoodsCoverByGoodsID", produces = MediaType.IMAGE_JPEG_VALUE)
    public byte[] getGoodsCoverByGoodsID(@RequestParam("id") Integer goods_id)
    {
        QueryWrapper<GoodsPicture> qw_goods_picture = new QueryWrapper<>();
        qw_goods_picture.eq(GoodsPicture.Column.GoodsID.getName(), goods_id);
        qw_goods_picture.eq(GoodsPicture.Column.Order.getName(), 0);
        var gp = this.dao_goods_picture.selectOne(qw_goods_picture);
        if (gp == null)
            return null;
        return gp.getContent();
    }
}


